package com.jrx.anytxn.reporting.job.unionFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.unionFile.step.LoanProductFileProcessor;
import com.jrx.anytxn.reporting.job.unionFile.step.LoanProductFileReader;
import com.jrx.anytxn.reporting.job.unionFile.step.LoanProductFileWriter;
import com.jrx.anytxn.reporting.job.unionFile.step.UnionLoanProductFileListener;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成借据产品信息文件(增量抽取 业务日期)
 * lbz
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateLoanProductFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GenerateLoanProductFileConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private UnionLoanProductFileListener unionLoanProductFileListener;
    @Autowired
    private LoanProductFileProcessor loanProductFileProcessor;
    @Autowired
    private LoanProductFileWriter loanProductFileWriter;


    @Bean
    public Job generateLoanProductFileJob() {
        return jobBuilderFactory.get("generateLoanProductFileJob").incrementer(new RunIdIncrementer()).listener(unionLoanProductFileListener)
                .start(generateLoanProductFileStep())
                .build();
    }

    @Bean
    public Step generateLoanProductFileStep(){
        return stepBuilderFactory.get("generateLoanProductFileStep")
                .<AmLoanMainInfo, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(loanProductFileReader())
                .processor(loanProductFileProcessor)
                .writer(loanProductFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<AmLoanMainInfo> loanProductFileReader(){
        logger.info("联盟借据产品信息表批量,机构号：{}", ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);

        return new LoanProductFileReader(sqlSessionFactory,anyTxnBatchProperties.getFileConfig().getPageSize(),org);
    }
}
